package com.scut.scutzone.controller;

import com.scut.scutzone.config.HttpSessionAttribute;
import com.scut.scutzone.config.ScutZoneConfig;
import com.scut.scutzone.service.DatabaseService;
import com.scut.scutzone.util.FileUtil;
import com.scut.scutzone.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private DatabaseService databaseService;
    @Autowired
    private ScutZoneConfig scutZoneConfig;

    @GetMapping("")
    @ResponseBody
    public Result getComments(@RequestParam(name = "parent_id") Integer parentId,
                              @RequestParam(name = "type") Integer type,
                              @RequestParam(name = "last_id", defaultValue = "0") Integer lastId,
                              @RequestParam(name = "limit", defaultValue = "5") Integer limit) {
        return Result.success(databaseService.getComments(parentId, type, lastId,limit));
    }

    @GetMapping("/{commentId}")
    @ResponseBody
    public Result getCommentInfo(@PathVariable Integer commentId,
                                 @RequestParam(name = "last_comment_id", defaultValue = "0") Integer lastCommentId,
                                 @RequestParam(name = "limit", defaultValue = "5") Integer limit) {
        return Result.success(databaseService.getCommentById(commentId,lastCommentId, limit));
    }

    @PostMapping("")
    @ResponseBody
    public Result saveComment(@RequestParam(name = "parent_id") Integer parentId,
                              @RequestParam(name = "type") Integer type,
                              @RequestParam(name = "content") String content,
                              @RequestParam(name = "img", required = false) MultipartFile img,
                              HttpSession session) throws IOException {
        String fileName = null;
        if (img != null) {
            fileName = FileUtil.saveImage(scutZoneConfig.getRoot_path()+scutZoneConfig.getComment_img_path(),
                                          img);
        }
        String uuid = (String) session.getAttribute(HttpSessionAttribute.USER_UUID);
        return Result.success(databaseService.saveComment(parentId, type, uuid, content, fileName));
    }

    @PutMapping("/{commentId}")
    @ResponseBody
    public Result updateComment(@PathVariable Integer commentId,
                                @RequestParam(value = "content") String content,
                                @RequestParam(value = "img") MultipartFile img,
                                HttpSession session) throws IOException {
        String fileName = null;
        if (img != null) {
            fileName = FileUtil.saveImage(scutZoneConfig.getRoot_path()+scutZoneConfig.getComment_img_path(),
                    img);
        }
        String uuid = (String) session.getAttribute(HttpSessionAttribute.USER_UUID);
        databaseService.updateComment(commentId, uuid, content, fileName);
        return Result.success(null);
    }


    @DeleteMapping("/{commentId}")
    @ResponseBody
    public Result updateComment(@PathVariable Integer commentId,
                                HttpSession session) {
        String uuid = (String) session.getAttribute(HttpSessionAttribute.USER_UUID);
        databaseService.deleteComment(commentId, uuid);
        return Result.success(null);
    }
}
